<?php
class region_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function getById($id=0)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_region` 
            WHERE 
                `id`='$id'
                AND `delete` is null
        ");
        return $query->result();
    }
    function getByParent($Parent=0)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_region` 
            WHERE 
                `parent`='$Parent'
                AND `delete` is null
        ");
        return $query->result();
    }
    function getByName($name="",$parent=0)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_region` 
            WHERE `name`='$name'
                AND `parent`='$parent'
            
            
        ");
        return $query->result();
    }
    function gets()
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_region` 
            #WHERE `delete` is null
            
        ");
        return $query->result();
    }
    function insert($params){
        $this->db->insert('dtb_region', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($id,$params){
        $this->db->where(array('id'=> $id));
        //$this->db->where('delete', 'is null',FALSE);
        $this->db->set('delete', 'null', FALSE);
        $this->db->update('dtb_region', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($id){
        $this->db->where(array('id'=> $id));
        //$this->db->where('delete', 'is null',FALSE);
        $this->db->delete('dtb_region'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
}
?>
